package org.huanletao.ssm.controller;

import com.github.pagehelper.Page;
import org.huanletao.ssm.model.PageInfo;
import org.huanletao.ssm.model.RespBean;
import org.huanletao.ssm.model.SysMenu;
import org.huanletao.ssm.model.User;
import org.huanletao.ssm.service.SysMenuService;
import org.huanletao.ssm.util.ShiroUtils;
import org.huanletao.ssm.util.TreeNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * </p>
 * package: org.huanletao.ssm.controller
 * description:
 * className: MenuController
 * author:  踏雪无痕
 * copyright: Copyright (c) 2022/8/28 17:12
 * since: 1.0.0
 */
@Controller
@RequestMapping("/system/menu/")
public class MenuController extends BaseController {

    private String prefix = "/system/menu/";
    private static final Logger logger = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    private SysMenuService sysMenuService;


    @GetMapping("/menus")
    public String menus() {
        return prefix + "menu";
    }


    @GetMapping("/menus/add")
    public String add(){
        return prefix+"add";
    }

    @GetMapping("/getAllMenus")
    @ResponseBody
    public RespBean<List<SysMenu>> getAllMenus() {
        User currentUser = ShiroUtils.getCurrentUser();
        logger.info("userinfo: {}", currentUser);
        List<SysMenu> sysMenus = sysMenuService.selectCurrentUserMenus(currentUser);
        return RespBean.ok("查询成功", sysMenus);
    }

    @PostMapping("/selectMenus")
    @ResponseBody
    public List<SysMenu> selectMenus() {
        List<SysMenu> sysMenuList = sysMenuService.findAll();
        return sysMenuList;
    }

    @PostMapping("/saveMenus")
    @ResponseBody
    public RespBean saveMenus(SysMenu sysMenu) {
        User currentUser = ShiroUtils.getCurrentUser();
        sysMenu.setCreateTime(new Date());
        sysMenu.setCreator(currentUser.getUsername());
        return toAjax(sysMenuService.save(sysMenu));
    }

    @PostMapping("deleteMenus")
    @ResponseBody
    public RespBean deleteMenus(Long id) {
        return toAjax(sysMenuService.delete(id));
    }

    @PostMapping("updateMenus")
    @ResponseBody
    public RespBean updateMenus(SysMenu sysMenu) {
        return toAjax(sysMenuService.update(sysMenu));
    }
}
